package cn.sun.videoplaydemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import org.yczbj.ycvideoplayerlib.constant.ConstantKeys;
import org.yczbj.ycvideoplayerlib.controller.VideoPlayerController;
import org.yczbj.ycvideoplayerlib.inter.listener.OnPlayerTypeListener;
import org.yczbj.ycvideoplayerlib.inter.listener.OnVideoBackListener;
import org.yczbj.ycvideoplayerlib.manager.VideoPlayerManager;
import org.yczbj.ycvideoplayerlib.player.VideoPlayer;

/**
 * 由于架构不支持x86 所以模拟器无法播放视频。
 */

public class MainActivity extends AppCompatActivity {

    VideoPlayer videoPlayer;
    ImageView ivBack;
    Button btCheck;
    TextView tvContent;
    boolean isDefaultUrl = false;
    String otherUrl = "http://vfx.mtime.cn/Video/2019/03/12/mp4/190312143927981075.mp4";
    String defaultUrl = "http://1.194.232.255/jy.lyjygy.com/public/uploads/sources/video/201907.mp4";
    EditText etUrl;
    Button btPlay;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        videoPlayer = findViewById(R.id.video_player);
        btCheck = findViewById(R.id.bt_check);
        tvContent = findViewById(R.id.tv_current);
        etUrl = findViewById(R.id.et_url);
        btPlay = findViewById(R.id.bt_play);
        btCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                VideoPlayerManager.instance().releaseVideoPlayer();
                isDefaultUrl = !isDefaultUrl;
                initAndPlay(null);
            }
        });
        btPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = etUrl.getText().toString().trim();
                if (url == null || url.length() == 0) {
                    Toast.makeText(MainActivity.this, "视频地址不能为空！", Toast.LENGTH_SHORT).show();
                    return;
                }
                initAndPlay(url);
            }
        });
        initAndPlay(null);
    }

    private void initAndPlay(String url) {
        initVideoPlayer(isDefaultUrl, url);
        showTextView(isDefaultUrl, url);
    }

    /**
     * 更新地址
     *
     * @param isDefault
     */
    private void showTextView(boolean isDefault, String url) {
        if (url != null && url.length() > 0) {
            tvContent.setText("当前选择的视频地址：" + url);
        } else {
            tvContent.setText("当前选择的视频地址：" + (isDefault ? defaultUrl : otherUrl));
        }
    }

    /**
     * 初始化视频播放器
     *
     * @param isDefault
     */
    private void initVideoPlayer(boolean isDefault, String url) {
        //设置播放类型
        //videoPlayer.setPlayerType(VideoPlayer.TYPE_NATIVE);
        videoPlayer.setPlayerType(ConstantKeys.IjkPlayerType.TYPE_IJK);
        //网络视频地址
        String videoUrl = isDefault ? defaultUrl : otherUrl;
        if (url != null && url.length() > 0) {
            videoUrl = url;
        }
        videoUrl = videoUrl.isEmpty() ? "" : App.getProxy(this).getProxyUrl(videoUrl);
        //设置视频地址和请求头部

        videoPlayer.setUp(videoUrl, null);
        //是否从上一次的位置继续播放
        videoPlayer.continueFromLastPosition(true);
        //设置播放速度
        videoPlayer.setSpeed(1.0f);
        //创建视频控制器
        VideoPlayerController controller = new VideoPlayerController(this);
        controller.setTitle("高仿优酷视频播放页面");
        controller.setLoadingType(ConstantKeys.Loading.LOADING_QQ);
        controller.setTopVisibility(false);
        controller.imageView().setVisibility(View.GONE);
        //controller.imageView().setBackgroundResource(R.color.blackText);
        controller.setOnVideoBackListener(new OnVideoBackListener() {
            @Override
            public void onBackClick() {
                onBackPressed();
            }
        });
        ivBack = controller.findViewById(R.id.back);

        controller.setOnPlayerTypeListener(new OnPlayerTypeListener() {
            @Override
            public void onFullScreen() {
                ivBack.setVisibility(View.VISIBLE);
            }

            @Override
            public void onTinyWindow() {
                ivBack.setVisibility(View.GONE);
            }

            @Override
            public void onNormal() {
                ivBack.setVisibility(View.GONE);
            }
        });
        //设置视频控制器
        videoPlayer.setController(controller);
        //隐藏分享按钮
        controller.findViewById(R.id.iv_share).setVisibility(View.GONE);
        ivBack.setVisibility(View.GONE);
    }

    @Override
    protected void onStop() {
        super.onStop();
        VideoPlayerManager.instance().releaseVideoPlayer();
    }


    @Override
    public void onBackPressed() {
        if (VideoPlayerManager.instance().onBackPressed()) {
            return;
        }
        super.onBackPressed();
    }

}
